package com.javaweb.secondhand_trading.mapper;

import com.javaweb.secondhand_trading.Dto.OrderDto;
import com.javaweb.secondhand_trading.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrderMapper {

    // 创建订单
    @Insert("INSERT INTO orders (product_id, buyer_id, seller_id, phone, status, created_at) " +
            "VALUES (#{productId}, #{buyerId}, #{sellerId}, #{phone}, #{status}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")
    int insert(Order order);


    // 更新订单状态
    @Update("UPDATE orders SET status = #{status} WHERE order_id = #{orderId}")
    int updateStatus(@Param("orderId") Integer orderId, @Param("status") String status);


    @Delete("DELETE FROM orders WHERE order_id = #{orderId}")
    int deleteOrders(@Param("orderIds") Integer orderIds);


    // 根据ID查询订单详情
    @Select("SELECT * FROM orders WHERE order_id = #{orderId}")
    @Results({
            @Result(property = "product", column = "product_id",
                    one = @One(select = "com.javaweb.secondhand_trading.mapper.ProductMapper.findById")),
            @Result(property = "buyer", column = "buyer_id",
                    one = @One(select = "com.javaweb.secondhand_trading.mapper.UserMapper.findById")),
            @Result(property = "seller", column = "seller_id",
                    one = @One(select = "com.javaweb.secondhand_trading.mapper.UserMapper.findById"))
    })
    OrderDto selectById(@Param("orderId") Integer orderId);


    // 查询买家的订单列表
    @Select("SELECT * FROM orders WHERE buyer_id = #{buyerId} ORDER BY created_at DESC")
    List<Order> selectByBuyerId(@Param("buyerId") Integer buyerId);


    // 查询卖家的订单列表
    @Select("SELECT * FROM orders WHERE seller_id = #{sellerId} ORDER BY created_at DESC")
    List<Order> selectBySellerId(@Param("sellerId") Integer sellerId);
}
